var t = document.title, ti;
//分区信息
var areadata = [];
$(function() {
    SG.time.update("#servertime", SG.config.servertime, SG.config.offsetseconds, false);
    //判断顶部状态
    if (SG.tools.cookie("topnav") == 'none') {
        $("#topnav").hide();
    }
    //状态左侧栏状态
    if (SG.tools.cookie("navstatus") == 'none') {
        $("#frmtitle").hide();
        $("#switch").attr("src", $("#switch").attr("src").replace("right", "left")).attr("title", LANG.JS_INDEX_SIDEBAR_SHOW);
    }
    //IE重置底部高度
    if ($.browser.msie) {
        $("#maintable").height($(document.body).height() - 42);
        $("#maininnertable").height($(document.body).height() - 100);
    }
    //左侧获得焦点
    $(window.frames['left']).focus();
    if (!$.browser.msie) {
        $(window.frames['left']).blur(function() {
            $(window.frames['left'].document).find(".e").fadeOut()
        });
    }
    //绑定keydown
    $(document.body).bind('keydown', function(e) {
        e = e || window.event;
        if (e.keyCode == 27) {
            //Space
            $(window.parent.frames['left']).focus();
            $(window.parent.frames['left'].document).find(".e").fadeIn();
        } else if (e.keyCode == 113) {
            //F2
            search();
        }
    });
    $(document.body).bind('keypress', function(e) {
        if (e.shiftKey && e.which == 90) {
            search();
        }
    });
    //展开
    $("#expandall ul li a").live("click", function() {
        $(window.frames['left'].document).find("ul.menu li a").removeClass("clicked");
        var lia = $(window.frames['left'].document).find("ul.menu li a[href=" + $(this).attr("href") + "]");
        $(window.frames['left'].document).find(".content").slideUp({duration: 800, easing: 'easeOutExpo'});
        lia.addClass("clicked").parent().parent().parent().slideDown({duration: 800, easing: 'easeOutExpo'});
        navadd($(this).text(), $(this).attr("href"));
        return false;
    });
    //切换侧边栏
    $("#switch").live("click", function() {
        var obj = $("#frmtitle");
        var cur = $(this);
        if (obj.css("display") == "none") {
            obj.show();
            cur.attr("src", cur.attr("src").replace("left", "right")).attr("title", LANG.JS_INDEX_SIDEBAR_HIDE);
        } else {
            obj.hide();
            cur.attr("src", cur.attr("src").replace("right", "left")).attr("title", LANG.JS_INDEX_SIDEBAR_SHOW);
        }
        SG.tools.cookie("navstatus", $("#frmtitle").css("display"));
    });
    //TAB点击切换事件
    $("ul.nav li a").live("click", function() {
        if (!$(this).hasClass("current")) {
            SG.tools.cookie("lastnavtext", $("ul.nav li a.current").attr("title"));
            $("ul.nav li a").removeClass("current");
            $(this).addClass("current");
            var title = $(this).attr("title");
            $("#frametd").find("iframe[title!=" + title + "]").attr("name", "mainhide").hide();
            $("#frametd").find("iframe[title=" + title + "]").attr("name", "main").show();
            SG.tools.cookie("navtext", title);
            SG.tools.cookie("navhref", $("#frametd").find("iframe[title=" + title + "]").attr("src"));
        }
        return false;
    }).live("dblclick", function() {
        //双击关闭事件
        if ($(this).attr("title") == LANG.HOME) {
            alert(LANG.JS_FORBIDDEN_CLOSE_LAST_TAB);
            return false;
        }
        $(this).next().click();
    });
    //点击关闭按钮
    $("ul.nav li b").live("click", function() {
        var prev = $(this).parent().prev().find("a");
        var title = $(this).prev().attr("title");
        if (!$(this).prev().hasClass("current")) {
            $(this).parent().remove();
            $("#frametd").find("iframe[title=" + title + "]").remove();
        } else {
            $("ul.nav li a").removeClass("current");
            var lastnavtext = SG.tools.cookie("lastnavtext");
            var lastnavobj = $("#frametd").find("iframe[title=" + lastnavtext + "]");
            if (lastnavtext && lastnavobj.size() > 0 && lastnavtext != title) {
                $("ul.nav li a[title='" + lastnavtext + "']").addClass("current");
                $("iframe[title=" + lastnavtext + "]").attr("name", "main").show();
            } else {
                prev.addClass("current");
                $("iframe[title=" + prev.attr("title") + "]").attr("name", "main").show();
            }
            $("#frametd").find("iframe[title=" + title + "]").remove();
            $(this).parent().remove();
            if ($("ul.nav li a.current").attr("title") == LANG.HOME) {
                SG.tools.cookie("navtext", '');
                SG.tools.cookie("navhref", '');
                SG.tools.cookie("lastnavtext", '');
            }
        }
    });
    //刷新页面后定位到之前最后进入的选项卡
    var text = SG.tools.cookie("navtext");
    var href = SG.tools.cookie("navhref");
    if (text && href && text != LANG.HOME) {
        //自动加载刷新前最后一个窗口,并显示左侧的列表框
        $("#frmleft").load(function() {
            var cur = window.frames["left"].$("ul.menu li a[rel='" + text + "']");
            cur.click();
        });
        //navadd(text,href);
    }
    $("#platlist").hover(function() {
        $("ul", this).show();
    }, function() {
        $("ul", this).hide();
    }).click(function() {
        $("ul", this).toggle();
    });

});
//快速直达
function search(x, y, keyword) {
    if ($("#searchme:visible").size() > 0) {
        $("#searchme").hide();
    } else {
        $("#search").data("keyword", keyword);
        $.dialog({id: 'searchme', title: LANG.SEARCH, width: 600, height: 500, x: x, y: y, type: 'iframe', content: 'common/search', style: 'no_button'});
    }
}
$("#top").click(function() {
    $("#topnav").toggle();
    SG.tools.cookie("topnav", $("#topnav").css("display"));
});
//添加链接到选项卡
function navadd(text, href) {
    SG.tools.cookie("navtext", text);
    SG.tools.cookie("navhref", href);
    SG.tools.cookie("lastnavtext", $("ul.nav li a.current").attr("title"));
    var nav = $(".nav");
    var frm = $("#frametd");
    var curnav = nav.find("li a[title=" + text + "]");
    var curfrm = frm.find("iframe[title=" + text + "]");
    nav.find("li a").removeClass("current");
    frm.find("iframe").attr("name", "mainhide").hide();
    var title = curnav.text();
    if (title == '') {
        nav.append("<li><a href=\"" + href + "\" title='" + text + "' class='current'><span></span>" + text + "</a><b>×</b></li>");
        frm.append("<iframe class='main_iframe' title='" + text + "' name='main' src=\"" + href + "\" scrolling='auto' frameborder=0 allowtransparency></iframe>");
    } else {
        curnav.addClass("current");
        curfrm.attr("src", href).attr("name", "main").show();
    }
    return false;
}
var asyncdata = [];
//同步到分区
function async(title, areainfo, data, tag) {
    asyncdata[tag] = [areainfo, data];
    if ($("#asyncdialog").size() == 0) {
        var content = '<div class="asyncdiv"><div class="tips">' + LANG.JS_SYNC_TIPS + '</div>';
        content += '<ul class="status"><li style="color:blue;"><em>' + LANG.JS_SYNC_NAME + '</em><ins>' + LANG.JS_SYNC_TITLE + '</ins><b>' + LANG.STATUS + '</b></li></ul></div>';
        $.dialog({id: "asyncdialog", title: LANG.JS_SYNC_SENDING_TIPS, content: content, x: 'right', y: 'bottom', fixed: true, width: 500, height: 400, style: "no_button"});
    }
    $.each(areainfo, function(i, j) {
        $("#asyncdialog .status").append("<li id='state" + tag + i + "' tag='" + tag + "' rel='" + i + "'><em><a href='" + j.admin + "' target='_blank'>" + j.name + "</a></em><ins>" + title + "</ins><b>发送中</b></li>");
        $.ajax({
            type: "POST",
            data: data,
            url: j.url,
            dataType: 'json',
            success: function(ret) {
                var t;
                if (ret && ret.code == 1) {
                    t = "<font color=green>" + LANG.JS_SYNC_SUCCESS + "</font>";
                } else {
                    t = "<a href='javascript:;' title='" + LANG.UNKNOWN + "'><font color=purple>" + LANG.JS_SYNC_FAILURE + "</font></a>";
                }
                $(".asyncdiv .status #state" + tag + i + " b").html(t);
            },
            error: function(e) {
                var text = $("*", e.response).parent().text();
                $(".asyncdiv .status #state" + tag + i + " b").html("<a href='javascript:;' title='" + LANG.STATUS + ":" + e.status + "&#13" + LANG.JS_SYNC_RESPONSE + ":" + text + "'><font color=red>" + LANG.JS_SYNC_FAILURE + "</font></a>");
            }
        });
    });
    if ($(".asyncdiv ul li").size() >= 15) {
        $(".asyncdiv ul li").each(function() {
            if ($("b", this).text() == LANG.JS_SYNC_SUCCESS) {
                $(this).remove();
            }
        });
    }
}
//点击重试
$(".asyncdiv .status b a").live("click", function() {
    var p = $(this).closest("b");
    var i = parseInt(p.closest("li").attr("rel"));
    var tag = p.closest("li").attr("tag");
    p.html(LANG.JS_SYNC_SENDING);
    $.ajax({
        type: "POST",
        data: asyncdata[tag][1],
        url: asyncdata[tag][0][i].url,
        dataType: 'json',
        success: function(ret) {
            var t;
            if (ret && ret.code == 1) {
                t = "<font color=green>" + LANG.JS_SYNC_SUCCESS + "</font>";
            } else {
                t = "<a href='javascript:;' title='" + LANG.UNKNOWN + "'><font color=purple>" + LANG.JS_SYNC_FAILURE + "</font></a>";
            }
            p.html(t);
        },
        error: function(e) {
            var text = $("*", e.response).parent().text();
            p.html("<a href='javascript:;' title='" + LANG.STATUS + ":" + e.status + "&#13" + LANG.JS_SYNC_RESPONSE + ":" + text + "'><font color=red>" + LANG.JS_SYNC_FAILURE + "</font></a>");
        }
    });
});
//FAV在线统计
function online_count(num) {
    var canvas = document.createElement('canvas'),
            ctx,
            img = document.createElement('img'),
            link = document.getElementById('favicon').cloneNode(true);

    if (canvas.getContext) {
        canvas.height = canvas.width = num < 100 ? 16 : 21; // set the size
        ctx = canvas.getContext('2d');
        img.onload = function() { // once the image has loaded
            ctx.drawImage(this, 0, 0);
            ctx.font = '14px "arial", sans-serif';
            ctx.fillStyle = '#ffffff';
            if (num < 10) {
                ctx.fillText(num, 5, 12);
            } else if (num < 100) {
                ctx.fillText(num, 1, 12);
            } else {
                ctx.fillText(num, 5, 15);
            }
            link.href = canvas.toDataURL('image/png');
            if ($("#favicon").size() > 0) {
                $("#favicon").attr("href", canvas.toDataURL('image/png'));
            } else {
                document.body.appendChild(link);
            }
        };
        img.src = 'images/image.png';
    }
}
online_count('M');
//窗口放大缩小事件
swfobject.embedSWF(SG.config.res_url + "/swf/zoom.swf?onchange=SG.tools.zoomChange", "zoomstate", "10", "10", "8", SG.config.res_url + "/swf/expressInstall.swf", {}, {wmode: 'transparent', allowScriptAccess: 'always', scale: 'noScale'}, {});